package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import apps.softtech.Utility.ApplicationConstants;
import apps.softtech.fastmarket.R;
import apps.softtech.fastmarket.model.ChatLog;

@SuppressLint("ServiceCast")
public class AdapterChat extends BaseAdapter{

	/**
	 * list chat
	 */
	ArrayList<ChatLog> mListChat = new ArrayList<ChatLog>();
	/**
	 * context
	 */
	Context mContext;
	/**
	 * layout inflater
	 */
	LayoutInflater inf;
	/**
	 * constructor
	 */
	public AdapterChat(Context mContext, ArrayList<ChatLog> mList){
		this.mContext = mContext;
		this.mListChat = mList;
		inf = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	@Override
	public int getCount() {
		return mListChat.size();
//		return 3;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View vi, ViewGroup arg2) {
//		ViewHolder vh = null;
//		if(vi==null){
//			vh = new ViewHolder();
//			vi = inf.inflate(R.layout.item_chat, null);
//			initViewHolder(vh,vi);
//			vi.setTag(vh);
//		}else{
//			vh = (ViewHolder) vi.getTag();
//		}
//		if(mListChat.get(position).isChatMe()){
//			vh.layoutChatMe.setLayoutParams(paramsChat);
//			vh.layoutChatYou.setLayoutParams(paramsNoChat);
//			vh.ivChatMe.setBackgroundResource(R.drawable.ic_launcher);
//			vh.tvChatMeContent.setText(""+mListChat.get(position).getmChatContent());
//			vh.tvChatMeDate.setText(""+mListChat.get(position).getmDate());
//		}else{
//			vh.layoutChatYou.setLayoutParams(paramsChat);
//			vh.layoutChatMe.setLayoutParams(paramsNoChat);
//			vh.ivChatYou.setBackgroundResource(R.drawable.ic_launcher);
//			vh.tvChatYouContent.setText(""+mListChat.get(position).getmChatContent());
//			vh.tvChatYouDate.setText(""+mListChat.get(position).getmDate());
//		}
		if(mListChat.get(position).getFromUserId()==ApplicationConstants.MY_USER){
			vi = inf.inflate(R.layout.item_chat, null);
			((ImageView) vi.findViewById(R.id.ivChatMe))
					.setBackgroundResource(R.drawable.ic_launcher);
			((TextView) vi.findViewById(R.id.tvChatMeContent)).setText(""
					+ mListChat.get(position).getContent());
			((TextView) vi.findViewById(R.id.tvChatMeDate)).setText(""
					+ mListChat.get(position).getCreatedDate());
//			ivChatMe.setBackgroundResource(R.drawable.ic_launcher);
//			tvChatMeContent.setText(""+mListChat.get(position).getmChatContent());
//			tvChatMeDate.setText(""+mListChat.get(position).getmDate());
		}else{
			vi = inf.inflate(R.layout.item_chat_you, null);
			((ImageView) vi.findViewById(R.id.ivChatYou))
					.setBackgroundResource(R.drawable.ic_launcher);
			((TextView) vi.findViewById(R.id.tvChatYouContent)).setText(""
					+ mListChat.get(position).getContent());
			((TextView) vi.findViewById(R.id.tvChatYouDate)).setText(""
					+ mListChat.get(position).getCreatedDate());
		}
		return vi;
	}
	/**
	 * init
	 * @param vh
	 * @param vi
	 */
	private void initViewHolder(ViewHolder vh,View vi) {
		paramsChat = new LinearLayout.LayoutParams(
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);
		paramsNoChat = new LinearLayout.LayoutParams(0, 0);
		//
		//init chat you
		//
		vh.ivChatYou=(ImageView)vi.findViewById(R.id.ivChatYou);
		vh.layoutChatYou = (LinearLayout)vi.findViewById(R.id.layoutChatYou);
		vh.tvChatYouContent = (TextView)vi.findViewById(R.id.tvChatYouContent);
		vh.tvChatYouDate = (TextView)vi.findViewById(R.id.tvChatYouDate);
		//
		//init chat me
		//
		vh.ivChatMe=(ImageView)vi.findViewById(R.id.ivChatMe);
		vh.layoutChatMe = (LinearLayout) vi.findViewById(R.id.layoutChatMe);
		vh.tvChatMeContent = (TextView) vi.findViewById(R.id.tvChatMeContent);
		vh.tvChatMeDate = (TextView)vi.findViewById(R.id.tvChatMeDate);
	}
	class ViewHolder{
		//
		//init chat you
		//
		LinearLayout layoutChatYou;
		TextView tvChatYouContent,tvChatYouDate;
		ImageView ivChatYou;
		//
		//init chat me
		//
		LinearLayout layoutChatMe;
		TextView tvChatMeContent,tvChatMeDate;
		ImageView ivChatMe;
	}
	/**
	 * update item
	 * @param item
	 */
	public void updateData(ArrayList<ChatLog> item){
		mListChat=item;
		notifyDataSetChanged();
	}
	LinearLayout.LayoutParams paramsChat ;
	LinearLayout.LayoutParams paramsNoChat;

}
